package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.VipCard;
import com.tbit.uqbike.client.pojo.vo.VipLogDot;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.service.VipCardLogService;
import com.tbit.uqbike.client.service.VipCardService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.DateTimeUtil;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员卡
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 10:00 2020/7/7
 */
@RestController
@RequestMapping("/vipCard")
public class VipCardController {
    @Autowired
    private VipCardLogService vipCardLogService;
    @Autowired
    private VipCardService vipCardService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询区域会员卡套餐
     *
     * @param adAccountId 区域id
     * @param pageNO      页码数
     * @param rowCount    每页大小
     * @param request
     * @param response
     */
    @RequestMapping("/queryPage")
    public void queryPage(Integer adAccountId, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount,
                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Assert.notNull(adAccountId, "-10001", "区域id不能为空");
            List<VipCard> list = vipCardService.queryPage(adAccountId, pageNO, rowCount);
            Integer total = vipCardService.queryCount(adAccountId);
            List<String> name = new ArrayList<>();
            if (list.size() > 0) {
                name.add("全车型");
                for (VipCard vip : list) {
                    if (vip.getFeeName() != null && !name.contains(vip.getFeeName())) {
                        name.add(vip.getFeeName());
                    }
                    if (vip.getFeeName() == null) {
                        vip.setFeeName("全车型");
                    }
                }
            }
            map.put("name", name);
            map.put("total", total);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, list);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 用户购买会员卡记录
     *
     * @param adAccountId 区域id
     * @param pageNO      页码数
     * @param rowCount    每页大小
     * @param token
     * @param request
     * @param response
     */
    @RequestMapping("/userVipPage")
    public void userVipPage(Integer adAccountId, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount, String token,
                            HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Assert.notNull(adAccountId, "-10001", "区域id不能为空");
            User user = tokenService.getUser(token);
            List<VipLogDot> list = vipCardLogService.queryPage(adAccountId, user.getUserId(), pageNO, rowCount);
            if (list.size() > 0) {
                for (VipLogDot vip : list) {
                    vip.setIsValid(1);
                    if (!DateTimeUtil.isDepositValid(vip.getExpireDate())) {
                        vip.setIsValid(0);
                    } else if (DateTimeUtil.getTimeDF(vip.getEffectDate() + " 00:00:00", DateTimeUtil.getNowTime()) < 0) {
                        vip.setIsValid(2);
                    }
                    if (vip.getFeeName() == null) {
                        vip.setFeeName("全车型");
                    }
                }
            }
            Integer total = vipCardLogService.queryCount(adAccountId, user.getUserId());
            map.put("total", total);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, list);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

}
